iT邦幫忙

2024 iThome 鐵人賽

DAY 23
0
Modern Web

asp.net core 分層架構快速上手系列 第 24

Day23 建立分店(建立View)

  • 分享至 

  • xImage
  •  
  • Area新增Store
    • 複製Category資料夾與CategoryController
    • Contorller用Store取代Category
    • 修改Upsert
      • 移除驗證。
 public StoreController(IUnitOfWork unitOfWork)
 {
     _unitOfWork = unitOfWork;
 }
 public IActionResult Index()
 {
     List<Store> objStoreList = _unitOfWork.Store.GetAll().ToList();
     return View(objStoreList);
 }

 public IActionResult Upsert(int? id)
        {

            if (id == null || id == 0)
            {
                return View(new Store());
            }
            else
            {
                Store storeObj = _unitOfWork.Store.Get(u => u.Id == id);
                return View(storeObj);
            }
        }
[HttpPost]
public IActionResult Upsert(Store storeObj)
{
    if (ModelState.IsValid)
    {
        if (storeObj.Id == 0)
        {
            _unitOfWork.Store.Add(storeObj);
            TempData["success"] = "分店新增成功!";
        }
        else
        {
            _unitOfWork.Store.Update(storeObj);
            TempData["success"] = "分店更新成功!";
        }
        _unitOfWork.Save();
        return RedirectToAction("Index");
    }
    return View(storeObj);
}
  • 修改Sotre Idex
@model IEnumerable<ClothesMall.Models.Store>
<p>
    <a asp-controller="Store" asp-action="Upsert" class="btn btn-primary">
        <i class="bi bi-plus-circle"></i> 新增分店
    </a>
</p>
<table class="table" id="storeTbl">
    <thead>
        <tr>
            <th>@Html.DisplayNameFor(model => model.Name)</th>
            <th>@Html.DisplayNameFor(model => model.Address)</th>
            <th>@Html.DisplayNameFor(model => model.City)</th>
            <th>@Html.DisplayNameFor(model => model.PhoneNumber)</th>
            <th>@Html.DisplayNameFor(model => model.Description)</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td> @Html.DisplayFor(modelItem => item.Name)</td>
                <td>@Html.DisplayFor(modelItem => item.Address)</td>
                <td>@Html.DisplayFor(modelItem => item.City)</td>
                <td>@Html.DisplayFor(modelItem => item.PhoneNumber)</td>
                <td>@Html.DisplayFor(modelItem => item.Description)</td>
                <td>
                    <a asp-action="Upsert" asp-route-id="@item.Id" class="btn btn-primary mx-2"><i class="bi bi-pencil-square"></i>Edit</a> |
                    <button type="button" class="btn btn-danger mx-2 delete-btn" data-id="@item.Id">
                        <i class="bi bi-trash-fill"></i>Delete
                    </button>
                </td>
            </tr>
        }
    </tbody>
</table>
@section Scripts {
    <script>
        $(document).ready(function () {
            $(".delete-btn").click(function () {
                var id = $(this).data("id");
                if (confirm("Are you sure you want to delete this Store?")) {
                    $.ajax({
                        url: "@Url.Action("Delete", "Store")",
                        type: "POST",
                        data: { id: id },
                        success: function () {
                            location.reload();
                        }
                    });
                }
            });
        });
    </script>
}

上一篇
Day22 建立分店(建立資料)
下一篇
Day24 建立分店(Upsert設置)
系列文
asp.net core 分層架構快速上手31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言